package com.clwl.supervise.superviseapp.util;

import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.List;


public class MoneyUtil {


    public static String[] rmbNumbers = new String[]{"一", "二", "三", "四", "五", "六", "七", "八", "九", "两", "廿", "卅", "○"};

    public static String[] bigNumbers = new String[]{"壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖", "俩", "廿", "卅", "零"};//大写的汉字

    public static Long[] tonumbers = new Long[]{1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 2L, 2L, 3L, 0L};//转化为阿拉伯数字


    public static String[] rmbMult = new String[]{"亿", "万", "千", "百", "十", "元", "角", "分"};//大写中间隔的倍数

    public static String[] bigRmbMult = new String[]{"億", "萬", "仟", "佰", "拾", "元", "角", "分"};


    public static Double[] toMult = new Double[]{100000000.0, 10000.0, 1000.0, 100.0, 10.0, 1.0, 0.0, 0.00};//转化为阿拉伯的倍数

    public static String convertAmount(String content) {
        //String str = "罚款贰仟伍佰肆拾叁元柒角壹分";
        // String str="1.罚款4000元；  ";
        if(content.indexOf(".")>-1){
            content = content.substring(content.indexOf(".", 1) + 1, content.length());
        }else if(content.indexOf("、")>-1){
            content = content.substring(content.indexOf("、", 1) + 1, content.length());
        } else if(content.indexOf("；")>-1){
            content = content.substring(content.indexOf("；", 1) + 1, content.length());
        }else{
            content = content.substring(content.indexOf(";", 1) + 1, content.length());
        }
        System.out.println("=====content===="+content);

        String fraction[] = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖",
                "拾", "佰", "仟", "元", "角", "分", "万", "亿"};
        String nm[] = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9","."};
        StringBuffer nmbuffer = new StringBuffer();
        StringBuffer dxbuffer = new StringBuffer();
        StringBuffer xxbuffer = new StringBuffer();
        List<String> dxlist = Arrays.asList(fraction);
        List<String> nmlist = Arrays.asList(nm);

        for (int i = 0; i < content.length(); i++) {
            String singleStr = String.valueOf(content.charAt(i));
            if (dxlist.contains(singleStr)) {
                dxbuffer.append(singleStr);
            }
            if (nmlist.contains(singleStr)) {
                nmbuffer.append(singleStr);
            }

        }
//        System.out.println("大写：" + dxbuffer.toString());
//        System.out.println("大写转小写：" + xxbuffer.toString());
//        System.out.println("小写：" + nmbuffer.toString());
        if (dxbuffer.length() > 1) {
            return rmbBigToSmall(dxbuffer.toString());
        } else if (nmbuffer.length() > 0) {
            DecimalFormat df = new DecimalFormat("#.00");
            System.out.println("===nmbuffer==="+Double.parseDouble(nmbuffer.toString()));
            return df.format(Double.parseDouble(nmbuffer.toString())).toString();
        }
        return null;
    }

    public static String rmbBigToSmall(String money) {
        Double number = 0.0;
        //遍历倍数的中文词遍历的时候一定要注意 选取的倍数词为最后一个倍数词,此次遍历为第一次遍历
        for (int i = 0; i < rmbMult.length; i++) {
            int index = money.lastIndexOf(rmbMult[i]) == -1 ? money.lastIndexOf(bigRmbMult[i]) : money.lastIndexOf(rmbMult[i]);
            if (index >= 0) {
                String storeMult = money.substring(0, index);
                money = money.substring(index + 1);
                //System.out.println(rmbMult[i] + " " + toMult[i]);

                if ((storeMult == null || storeMult.length() <= 0) && toMult[i].intValue() == 10) {
                    number = number + toMult[i];
                } else {
                    number = number + (toMult[i] * getPrexNum(storeMult));
                }
            }
        }

        number = number + getNumByBig(money);
        DecimalFormat df = new DecimalFormat("#.00");
        //System.out.println(df.format(number));
        return df.format(number).toString();
    }

    private static Double getPrexNum(String storeMult) {
        Double result = 0.0;
        for (int i = 0; i < rmbMult.length; i++) {
            int index = storeMult.lastIndexOf(rmbMult[i]) == -1 ? storeMult.lastIndexOf(bigRmbMult[i]) : storeMult.lastIndexOf(rmbMult[i]);
            if (index >= 0) {
                String storeMult2 = storeMult.substring(0, index);
                storeMult = storeMult.substring(index + 1);
                if ((storeMult2 == null || storeMult2.length() <= 0) && toMult[i].intValue() == 10) {
                    result = result + toMult[i];
                } else {
                    result += getNumByBig(storeMult2) * toMult[i];
                }
            }
        }
        if (storeMult != null && storeMult.length() > 0) {
            result = result + getNumByBig(storeMult);
        }
        return result;
    }


    private static Long getNumByBig(String big) {
        Long result = 0L;
        for (int j = 0; j < rmbNumbers.length; j++) {
            big = big.replaceAll(rmbNumbers[j], tonumbers[j].toString());
            big = big.replaceAll(bigNumbers[j], tonumbers[j].toString());
        }
        try {
            result = Long.valueOf(big);
        } catch (Exception e) {
            result = 0L;
        }
        return result;
    }


    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println(rmbBigToSmall("壹万贰仟伍佰肆拾叁元柒角壹分"));
    }

}
